using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;

namespace ImageCSharp.Controls
{
    /// <summary>
    /// 历史记录面板控件
    /// </summary>
    public partial class HistoryPanel : UserControl, INotifyPropertyChanged
    {
        #region Dependency Properties

        public static readonly DependencyProperty HistoryItemsProperty =
            DependencyProperty.Register("HistoryItems", typeof(System.Collections.ObjectModel.ObservableCollection<HistoryItem>), typeof(HistoryPanel),
                new PropertyMetadata(null, OnHistoryItemsChanged));

        public System.Collections.ObjectModel.ObservableCollection<HistoryItem> HistoryItems
        {
            get => (System.Collections.ObjectModel.ObservableCollection<HistoryItem>)GetValue(HistoryItemsProperty);
            set => SetValue(HistoryItemsProperty, value);
        }

        #endregion

        public HistoryPanel()
        {
            InitializeComponent();
            DataContext = this;
            
            // 添加一些示例历史记录
            //LoadSampleHistory();
        }

        private void LoadSampleHistory()
        {
            var historyItems = new System.Collections.ObjectModel.ObservableCollection<HistoryItem>
            {
                new HistoryItem { Action = "打开图像", Icon = "FileOpen", IsActive = false, Timestamp = DateTime.Now.AddMinutes(-15) },
                new HistoryItem { Action = "调整大小", Icon = "Resize", IsActive = false, Timestamp = DateTime.Now.AddMinutes(-12) },
                new HistoryItem { Action = "应用滤镜", Icon = "Filter", IsActive = false, Timestamp = DateTime.Now.AddMinutes(-10) },
                new HistoryItem { Action = "绘制线条", Icon = "Brush", IsActive = true, Timestamp = DateTime.Now.AddMinutes(-5) }
            };

            HistoryListBox.ItemsSource = historyItems;
        }

        private static void OnHistoryItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is HistoryPanel panel)
            {
                if (e.NewValue is System.Collections.ObjectModel.ObservableCollection<HistoryItem> items)
                {
                    panel.HistoryListBox.ItemsSource = items;
                    
                    // 订阅集合变化事件
                    items.CollectionChanged += (sender, args) =>
                    {
                        panel.Dispatcher.Invoke(() =>
                        {
                            panel.HistoryListBox.Items.Refresh();
                        });
                    };
                }
                else
                {
                    panel.HistoryListBox.ItemsSource = null;
                }
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public class HistoryItem
    {
        public string Action { get; set; } = string.Empty;
        public string Icon { get; set; } = "Help";
        public bool IsActive { get; set; }
        public DateTime Timestamp { get; set; }
        
        public string Name => Action;
        public string TimeString => Timestamp.ToString("HH:mm:ss");
    }
}